The goal of this lab is to explore static tables using the grammar of tables from the gt and gtExtras package.
The gt() function creates a gt table object when provided with table data. Using this function is the first step in a typical gt workflow. Once we have the gt table object, we can perform styling transformations by adding on column, formatting, tab styling, and summary layers.
A few helpful links to provide more information about the gt package: gt rstudio and intro to gt
Posit (formally RStudio) hosts an annual table competition! Check out some past winning tables: 20212022
Dataset
We will be using the pizzaplace dataset included in the gt package, the house_prices, stream_data, and team_logo dataset which should be located in your /data subdirectory. Each dataset will require a bit of data wrangling provided in their respective exercise code chunk.
Use the pizza_summary dataset created below to recreate the following graphic as precisely as possible. We will work through this exercise together to provide a high level overview of a few of the ways you can layer on table elements to make a simple but visually appealing table.
Hints:
md(): Interpret input text as Markdown-formatted text
Using the house_illinois dataset derived from the house_price dataset, recreate the following graphic as precisely as possible.
Similar to the ggplot scales_*() function, the gt package has useful transformations that can be performed using the following syntax tab_*(), fmt_*(), cols_*(). Check the package documentation for a full list of options.
Adding icons and graphics to a table is made easy with gtExtras. Using the stream_data dataset, recreate the following graphic as precisely as possible.
Hints:
gt_plt_bullet()
nytimes theme()
fmt_symbol_first()
Solution
Code
# pipe the following code into your gt functionsstream_data |>mutate(type =map(type, fontawesome::fa) ) |>gt() |>gt_theme_nytimes() |>gt_plt_bullet(column = nominee, target = winner) |>fmt_symbol_first(column = ratio, suffix ="%",decimals =1, scale_by =100 ) |>gt_add_divider(type, color ="lightgrey", weight =px(1)) |>tab_header(title ="Netflix leads Emmy Nominations & Wins: 2020-2021",subtitle =md("but **HBO** leads in their Wins to Nominations ratio") ) |>gt_highlight_rows(rows = distributor =="HBO", fill ="grey", alpha =0.4) |>cols_width( distributor ~px(100), type ~px(40), ratio ~px(50), nominee ~px(230) )
Netflix leads Emmy Nominations & Wins: 2020-2021
but HBO leads in their Wins to Nominations ratio
distributor
type
ratio
nominee
Netflix
27.7%
HBO
28.8%
NBC
20.5%
ABC
16.0%
Hulu
2.0%
FX Networks
11.1%
Prime Video
8.9%
Apple TV+
23.8%
CBS
2.5%
Exercise 5
Using the team_logo dataset, recreate the following graphic as precisely as possible.
The tab_options() layer for gt is similar to the theme() option for ggplot. Here you can add customization to the background colors, line type, size, etc.
Hints:
gt_img_rows with height = 30
tab options:
data row padding is px(1)
table background color is "gray90"
table width is 100%
table font color is "red"
column labels have background color "black", font size "large", and font weight "bold",
table body has hlines color "red"
table body has border bottom color "red"
Solution
Code
# pipe the following code into your gt functionsteam_logo |> dplyr::select(logo = team_logo_espn, team_name, team_nick, team_conf) |>slice_head(n =6) |>gt() |>gt_img_rows(columns = logo, img_source ="web", height =30) |>tab_options(data_row.padding =px(1),table.background.color ="gray90",table.width =pct(100),table.font.color ="red",column_labels.background.color ="black",column_labels.font.size ="large",column_labels.font.weight ="bold",table_body.hlines.color ="red",table_body.border.bottom.color ="red" )